#define _CRT_SECURE_NO_WARNINGS	
                    
#include<stdio.h>  

void Swap(int* x, int* y)
{
    int tmp = *x;
    *x = *y;
    *y = tmp;
}

void adjust(int arr[], int start, int end)
{
    int largest = start;
    int left = start * 2 + 1;
    int right = start * 2 + 2;

    if (left <= end && arr[left] > arr[largest])
        largest = left;
    if (right <= end && arr[right] > arr[largest])
        largest = right;

    if (largest != start)
    {
        Swap(&arr[largest], &arr[start]);
        adjust(arr, largest, end);
    }
}

void Heap_Sort(int arr[],int len)
{          
    for (int i = (len - 2) / 2; i >= 0; i--)
        adjust(arr, i, len - 1);
    
    for (int i = 0; i < len - 1; i++)
    {
        Swap(&arr[0], &arr[len - 1 - i]);
        adjust(arr, 0, len - 1 - i - 1);
    }
}                                   
                                 
                                      
int main()
{
    int arr[] = { 7,6,4,10,3,1,2,5,9,8 };

    Heap_Sort(arr, 10);

    for (int i = 0; i < 10; i++)
        printf("%d ", arr[i]);

    return 0;
}